$(function () {
    if ("undefined" != typeof bnId && null != bnId && "" != bnId) {
        var temp = [];
        $("#privateKeyId").val(bnId);//主键ID
        $("#case").attr("data-tablename", table_name);//表名
        $("#case").attr("data-column", column_name);//字段名称
        $("#case").attr("data-columnone", column_nameone);//字段名称
        $("#case").attr("data-columntwo", column_nametwo);//字段名称
        $("#case").attr("data-size", dataSize);//图片大小
        $("#case").attr("data-height", dataHeight);//图片长度
        $("#case").attr("data-width", dataWidth);//图片高度
        $("#case").attr("data-num", dataNum);//图片数量
        $("#case").attr("data-typedir", typeDir);//图片数量
        $("#case").attr("data-uploadurl", operatorAdd);//添加操作
        $("#case").attr("data-deleteurl", operatorDel);//删除操作

        if ("undefined" != typeof itemList && null != itemList && "" != itemList) {
            $.each(itemList, function (index, item) {
                temp.push(item.imgUrl);
                var imgName = item.imgName;
                var imgNameStr = imgName.substring(0, imgName.lastIndexOf("."));
                var imgDes = $("#imgDes").clone().attr("id", "imgDes_" + imgNameStr);
                $("#imgDesDiv").append(imgDes);
                $("#imgDes_" + imgNameStr).find("#bannerItemId").val(item.id);
                $("#imgDes_" + imgNameStr).find("#imgName").html(imgName);
                $("#imgDes_" + imgNameStr).find("#targetUrl").val(item.targetUrl);
                $("#imgDes_" + imgNameStr).find("#bnImgDes").val(item.description);
            });
        }

        //初始化图片展示
        $("#case").attr("data-value", temp);
    }

    $("#case").upload(
        function (_this, data) {
            var imgNameStr = data.substring(0, data.lastIndexOf("."));
            var is = $("#imgDesDiv").children(".panel-body");
            $.each(is, function (i, item) {
                $(item).hide();
            });
            $("#imgDes_" + imgNameStr).show();
        }
    )

});

function setPageNumber(type) {
    var picType = $("#picType").val();
    var pageNumber = $("#pageNumber").val().split(";")[0];
    var picTypeFlag = $("#pageNumber").val().split(";")[1];
    if (picType != picTypeFlag) {
        pageNumber = 1;
    }

    if (type == '2') {
        pageNumber = Number(pageNumber) + 1;
    } else {
        //上一页
        if (pageNumber <= 1) {
            pageNumber = 1;
        }
        //下一页
        if (pageNumber > 1) {
            pageNumber = Number(pageNumber) - 1;
        }
    }
    $("#pageNumber").val(pageNumber + ";" + picType);
}

/**
 * 加载本地图片库图片
 */
function loadTypePics() {
    var picType = $("#picType").val(); //类型
    var pageNumber = $("#pageNumber").val().split(";")[0]; //页码
    var picTypeFlag = $("#pageNumber").val().split(";")[1];//类型
    if (picType != picTypeFlag) {
        pageNumber = 1;
    }
    //根据类型加载数据库中的图片
    var data = {};
    data.picType = picType;
    data.page = pageNumber;
    data.size = 8; //默认每页8个图片

    $.ajax({
        url: application.contextPath + "/common/queryTypePics",
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({parmsMap: data}),
        success: function (res) {
            $('#ulPic li').not(':first').not(':last').remove();
            for (var i = 0; i < res.data.rows.length; i++) {
                add(-1, res.data.rows[i].imgUrl);
            }
        }
    })
}

/**
 * 添加图片
 * @param n
 * @param url
 */
function add(n, url) {
    var ul = document.getElementById("ulPic");
    $('#ulPic li:last').prev('li').after("<li><a href='javascript:onClickAmage(\"" + url + "\");'><image style=\"padding-left:2px;\" src=\"" + url + "\" width=\"28px\" height=\"28px\"></image></a></li>");
}

/**
 * 上传图片
 * @param privateKeyId 主键ID
 * @param table_name  表名
 * @param column_name 字段名称
 * @returns {boolean} 布尔值
 */
function uploadPic(privateKeyId, table_name, column_name,column_nameone,column_nametwo) {
    //主键ID
    bnId = $("#" + privateKeyId).val();
    if (bnId == null || bnId == "" || bnId == "undefined") {
        layer.msg("请先保存基本名称信息！", {icon: 1});
        return false;
    }
    loadUtil = new Load($);
    loadUtil.load({
        container: '#homeContent',
        //跳转到上传图片公用页面
        url: application.contextPath + "/common/toUploadPage?bnId=" + bnId + "&table_name=" + table_name + "&column_name=" + column_name + "&dataSize=" + dataSize + "&dataHeight=" + dataHeight + "&dataWidth=" + dataWidth + "&dataNum=" + dataNum +"&typeDir="+typeDir+"&operatorAdd="+ operatorAdd+"&operatorDel="+operatorDel+"&column_nameone="+column_nameone+"&column_nametwo="+column_nametwo,
    });
}

/**
 * 预览图片
 * @param url
 */
function onClickAmage(url) {
    var img = new Image();
    img.src = url;
    window.open(application.contextPath + "/common/toYuLan?url=" + url + "&width=" + img.width + "&height=" + img.height, '预览图片', "height=300, width=700,left=300,right=300, top=300")
}